/*
 * AlbumThumbnailPanel.java
 *
 * Created on October 7, 2008, 2:09 PM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.music.exceptions.PlaylistException;
import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistAddAlbumAction;
import com.bff.slimserver.musicjukebox.lib.actions.AddFavoriteAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistInsertAlbumAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistLoadAlbumAction;
import com.bff.slimserver.musicjukebox.ui.components.CoverArtPanel;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.bff.slimserver.SlimPlaylist;
import org.bff.slimserver.musicobjects.SlimAlbum;

/**
 *
 * @author  bfindeisen
 */
public class AlbumThumbnailPanel extends javax.swing.JPanel implements MouseListener {

    private Container parent;
    private SlimAlbum album;
    private ThumbnailPanel thumbPanel;
    private SlimPlaylist playlist;
    private JPopupMenu menu;

    /**
     * Make sure to set preferred size before calling loadImage.
     * @param song
     */
    public AlbumThumbnailPanel(SlimAlbum song) {
        initComponents();
        parent = this;
        setAlbum(song);
        try {
            setPlaylist(JukeboxProperties.getInstance().getPlaylist());
        } catch (PlaylistException ex) {
            Logger.getLogger(AlbumThumbnailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        thumbPanel = new ThumbnailPanel();
        add(thumbPanel, BorderLayout.CENTER);

        lblAlbum.setText(getAlbum() != null ? getAlbum().getName() : "");
        lblArtist.setText(song.getArtist());
        menu = new JPopupMenu();
        menu.add(new JMenuItem(new PlaylistAddAlbumAction(getPlaylist(), getAlbum())));
        menu.add(new JMenuItem(new PlaylistInsertAlbumAction(getPlaylist(), getAlbum())));
        menu.add(new JMenuItem(new PlaylistLoadAlbumAction(getPlaylist(), getAlbum())));
        menu.add(new JMenuItem(new AddFavoriteAction(getAlbum())));

        thumbPanel.addMouseListener(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        detailPanel = new CoverArtPanel();
        lblAlbum = new javax.swing.JLabel();
        lblArtist = new javax.swing.JLabel();
        lblMessage = new javax.swing.JLabel();
        coverPanel = new javax.swing.JPanel();

        setBackground(ColorProperties.PANEL_COVER_ART_BACKGROUND);
        setLayout(new java.awt.BorderLayout());

        detailPanel.setLayout(new java.awt.GridBagLayout());

        lblAlbum.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblAlbum.setForeground(ColorProperties.PANEL_COVER_ART_FOREGROUND);
        lblAlbum.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAlbum.setText("jLabel1");
        lblAlbum.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        detailPanel.add(lblAlbum, gridBagConstraints);

        lblArtist.setForeground(ColorProperties.PANEL_COVER_ART_FOREGROUND);
        lblArtist.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblArtist.setText("jLabel2");
        lblArtist.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        detailPanel.add(lblArtist, gridBagConstraints);

        lblMessage.setForeground(ColorProperties.INFO_MESSAGE_FORGROUND);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        detailPanel.add(lblMessage, gridBagConstraints);

        add(detailPanel, java.awt.BorderLayout.SOUTH);

        javax.swing.GroupLayout coverPanelLayout = new javax.swing.GroupLayout(coverPanel);
        coverPanel.setLayout(coverPanelLayout);
        coverPanelLayout.setHorizontalGroup(
            coverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        coverPanelLayout.setVerticalGroup(
            coverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 263, Short.MAX_VALUE)
        );

        add(coverPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel coverPanel;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JLabel lblAlbum;
    private javax.swing.JLabel lblArtist;
    private javax.swing.JLabel lblMessage;
    // End of variables declaration//GEN-END:variables

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.isPopupTrigger()) {
            createPlayPopUp(e);
        } else {
            //if (e.getClickCount() == 2) {
            Container comp = parent;
            while (!(comp instanceof JFrame)) {
                comp = comp.getParent();
            }

            AlbumThumbnailDialog dialog = new AlbumThumbnailDialog((JFrame) comp, getAlbum());
            dialog.setVisible(true);
        }
    }

    public void mousePressed(MouseEvent e) {
        createPlayPopUp(e);
    }

    public void mouseReleased(MouseEvent e) {
        createPlayPopUp(e);
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void createPlayPopUp(MouseEvent e) {
        if (e.isPopupTrigger()) {
            menu.setVisible(true);
            menu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

    public void loadImage() {
        thumbPanel.loadImage(getAlbum());
        lblArtist.setText(getAlbum().getArtist());
        lblAlbum.setText(getAlbum().getName());
    }

    @Override
    public void setPreferredSize(Dimension dim) {
        thumbPanel.setPreferredSize(dim);
        super.setPreferredSize(new Dimension((int) dim.getWidth(), (int) dim.getHeight() + 40));
    }

    public SlimAlbum getAlbum() {
        return album;
    }

    public void setAlbum(SlimAlbum album) {
        this.album = album;
    }

    public SlimPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(SlimPlaylist playlist) {
        this.playlist = playlist;
    }
}
